1. Merge Sort
Merge sort is a recursive algorithm that continually splits a list in half. The key point of it is "divide and conquer". Here's the code:
Especially, quick sort is relatively low when processing a part-sorted list, but it is extremely good at sorting and completely random list.
3. Insertion Sort 1. a sorted list and the unsorted list 2. compare, make space and insert Here's the code:
def insertionSort(alist):
for index inrange(1,len(alist)):
currentvalue = alist[index]
position = index
while position>0and alist[position-1]>currentvalue:
alist[position]=alist[position-1]
position = position-1
alist[position]=currentvalue
alist =[54,26,93,17,77,31,44,55,20]
insertionSort(alist)print(alist)
4. Selection Sort 1. Compare 2. Swap Here's the code:
5. Bubble Sorts
1. Two numbers
2. Compare
...
3. The larger to the bottom
Here's the code:
def bubbleSort(alist):
for passnum inrange(len(alist)-1,0,-1):
for i inrange(passnum):
if alist[i]>alist[i+1]:
temp = alist[i]
alist[i]= alist[i+1]
alist[i+1]= temp
alist =[54,26,93,17,77,31,44,55,20]
bubbleSort(alist)print(alist)
2. Searching algorithms
1. linear search (waste of time) (not recommended)
Here's the code:
[[code]][[code format="python"]]def sequentialSearch(alist, item):
pos =0
found =Falsewhile pos <len(alist)andnot found:
if alist[pos]== item:
found =Trueelse:
pos = pos+1return found
testlist =[1,2,32,8,17,19,42,13,0]print(sequentialSearch(testlist,3))print(sequentialSearch(testlist,13))
2. Binary Search
Complexity: log2(N)
Key find the middle and cut the half we don't need
Here's the code:
1.Array (direct access/ continuous/ difficult to insert/remove) (index and content) Move every content downward by one
2.Linked list serial access not continuous ease to insert/ remove head-node (data and reference)-tail head and tail are references (Null) Insertion: 1.new node 2.new link 3.change link
3.Stack First in last out not continuous Push (insert) and Pop (remove) (check if the stack is already full or empty) Pointer (top-of-stack-pointer) Push and Pop: move the pointer Application: function main () Function1 () Function2 () Function3 () This is the process called "Call stack" Inversion of a list
4.Queue First in first out Tail-in, head-out ENQUEUE-tail-pointer (move to the next), DEQUEUE-head pointer (move to the next)
5.Tree: (binary tree- left and right) Root (node with one more reference), Internal node (has parent and children), leaf node (with parent but no children)
Binary Tree Left and right 5 17 36 Build a tree – insertion
Algorithms
Data Structure
Sorting Algorithms
Merge sort is a recursive algorithm that continually splits a list in half. The key point of it is "divide and conquer". Here's the code:
2. Quicksort
Steps:
1.pivot
2.partition
3.Quicksort (1, pivot - 1)
4.Quicksort (Pivot + 1, end)
5.Quicksort list (only one element)-sorted, recursion
Here's the code:
Especially, quick sort is relatively low when processing a part-sorted list, but it is extremely good at sorting and completely random list.
3. Insertion Sort
1. a sorted list and the unsorted list
2. compare, make space and insert
Here's the code:
4. Selection Sort
1. Compare
2. Swap
Here's the code:
5. Bubble Sorts
1. Two numbers
2. Compare
...
3. The larger to the bottom
Here's the code:
2. Searching algorithms
1. linear search (waste of time) (not recommended)Here's the code:
2. Binary Search
Complexity: log2(N)
Key find the middle and cut the half we don't need
Here's the code:
3. Data Structure
1.Array (direct access/ continuous/ difficult to insert/remove) (index and content)Move every content downward by one
2.Linked list
serial access
not continuous
ease to insert/ remove
head-node (data and reference)-tail
head and tail are references (Null)
Insertion:
1.new node
2.new link
3.change link
3.Stack
First in last out
not continuous
Push (insert) and Pop (remove) (check if the stack is already full or empty)
Pointer (top-of-stack-pointer)
Push and Pop: move the pointer
Application:
function main ()
Function1 ()
Function2 ()
Function3 ()
This is the process called "Call stack"
Inversion of a list
4.Queue
First in first out
Tail-in, head-out
ENQUEUE-tail-pointer (move to the next), DEQUEUE-head pointer (move to the next)
5.Tree: (binary tree- left and right)
Root (node with one more reference), Internal node (has parent and children), leaf node (with parent but no children)
Binary Tree
Left and right
5
17
36
Build a tree – insertion